#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    char shm_name[100];
    sprintf(shm_name,"/letter%d",getpid());
    int fid = shm_open(shm_name,O_RDWR | O_CREAT,0644);
    if(fid < 0 )
    {
        perror("open file");
        exit(EXIT_FAILURE);
    }
    
    ftruncate(fid,1024);
    
    char* share;
    share = mmap(NULL,1024,PROT_READ | PROT_WRITE,MAP_SHARED,fid,0);

    if(share == MAP_FAILED)
    {
        perror("mmap");
        exit(EXIT_FAILURE);
    }
    close(fid);

    
    pid_t pid = fork();
    if(pid == 0)
    {
        strcpy(share,"Hello brother\n");
        printf("child success\n");
    }else{
        waitpid(pid,NULL,0);
        printf("you say%s\n",share);
        munmap(share,1024);
    }

    shm_unlink(shm_name);
    return 0;
}
